package com.lin.service.MulThread;

import lombok.Data;

import java.util.concurrent.BlockingQueue;

@Data
public class Consumer implements Runnable{
    private BlockingQueue<Task> blockingQueue;

    public Consumer(BlockingQueue<Task> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        Task task;
        //当任务队列中还有需要执行的任务
        while((task = blockingQueue.poll()) != null){
            //业务执行
            try {
                Thread.sleep(task.getTime());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "完成任务:"+task.getName()+",耗时:"+task.getTime()+"毫秒");
        }
    }
}
